package com.numbfish.ch81_ex02_selenium_official_start.test;


import com.numbfish.ch81_ex02_selenium_official_start.DriverBase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.net.MalformedURLException;
import java.time.Duration;

public class BasicIT extends DriverBase {

    private RemoteWebDriver driver;

    @BeforeEach
    public void setup() throws MalformedURLException {
        driver = getDriver();
    }

    private void baiduExampleThatSearchesFor(final String searchString) {
        driver.get("https://www.baidu.com/");

        WebElement searchField = driver.findElement(By.name("wd"));

        searchField.clear();
        searchField.sendKeys(searchString);

        System.out.println("Page title is: " + driver.getTitle());

        searchField.submit();

        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        wait.until((ExpectedCondition<Boolean>) d -> d.getTitle().toLowerCase().startsWith(searchString));

        System.out.println("Page title is: " + driver.getTitle());
    }

    @Test
    public void googleCheeseExample() {
        baiduExampleThatSearchesFor("苹果香");
    }

}
